﻿/*
	flad : Copyright 2010 Dale J Williams

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/
package lads.flad.tween {
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.Dictionary;
	import flash.utils.getTimer;
	
	/**
	 * TweenManager: Manages Tween animation.
	 * @author Dale J Williams
	 * @version 2.0
	 */
	public class TweenManager {
		
		public var frames		:int;
		public var difference	:int;
		public var time			:int;
		public var fps			:Number;
		
		private var _sprite		:Sprite 	= new Sprite();
		private var _tweens		:Dictionary	= new Dictionary(false);
		
		public function TweenManager() {
			init();
		}
		
		private function init():void {
			_sprite.addEventListener(Event.ENTER_FRAME, update);
		}
		
		private function update(e:Event=null):void {
			frames++; 													
			difference = getTimer() - time; 						
			time += difference;						
			fps = frames / time * 1000;
			for each(var tween:Tween in _tweens)
				tween.update(time); 		
		}
		
		public function to($target:Object, $duration:Number, $properties:Object):Tween { 
			if (!$target) throw new Error("Can not Tween a null target.");
			if (_tweens[$target]) kill($target);
			var tween:Tween = _tweens[$target] = new Tween();	
			tween.manager = this;
			tween.init($target, $duration, $properties);
			return tween;
		}
		
		public function kill(target:Object):void {
			if (_tweens[target])
				delete _tweens[target];
		}
		
		public function isTweening(target:Object):Boolean {
			return _tweens[target] != null;
		}
		
		public function getTween(target:Object):Tween {
			return _tweens[target] || null;
		}
		
	}	
}